<?php
class Employeers_model extends CRM_Model{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function GetDS(){
		$this->db->select('_employeers.*, 
							DATE_FORMAT(_employeers.birthday, \'%d.%m.%Y\') as birthday,
							DATE_FORMAT(_employeers.bdate,  \'%d.%m.%Y\') as bdate,
							DATE_FORMAT(_employeers.edate,  \'%d.%m.%Y\') as edate,
							DATE_FORMAT(_employeers.modifyDT,  \'%d.%m.%Y %H:%i\') as modifyDT,
							_filials.name as filial_name');
		$this->db->from('_employeers');
		$this->db->join('_filials', '_filials.rid = _employeers._filials_rid');
		if($sort = $this->ciObject->GetSessionData('sort')){
			$this->db->orderby($sort['c'], $sort['r']);
		}
		$query = $this->DBGet('_employeers');
		if($query->num_rows()>0) return $this->ciObject->GetPagingDS($query->result());
		return array();
	}
	
	public function GetEditDS(){
		$this->db->select('_employeers.*,
							DATE_FORMAT(_employeers.birthday, \'%d.%m.%Y\') as birthday,
							DATE_FORMAT(_employeers.bdate,  \'%d.%m.%Y\') as bdate,
							DATE_FORMAT(_employeers.edate,  \'%d.%m.%Y\') as edate,
							DATE_FORMAT(_employeers.modifyDT,  \'%d.%m.%Y %H:%i\') as modifyDT,
							');
		$this->db->from('_employeers');
		$this->db->where(array('_employeers.rid'=>$this->ciObject->GetSessionData('activerecord')));
		$query = $this->DBGet('_employeers');
		if($query->num_rows()>0) return $query->row();
		return False;
	}
	
	public function CreateRecord(){
		$insArray = array('_filials_rid'=>$_POST['_filials_rid'],
							'f_name'=>$_POST['f_name'],
							's_name'=>$_POST['s_name'],
							'l_name'=>$_POST['l_name'],
							'f_name_lat'=>$_POST['f_name_lat'],
							'l_name_lat'=>$_POST['l_name_lat'],
							'birthday'=>$this->toMySQLDate($_POST['birthday']),
							'bdate'=>$this->toMySQLDate($_POST['bdate']),
							'edate'=>$this->toMySQLDate($_POST['edate']),
							'passp_seria'=>$_POST['passp_seria'],	
							'passp_num'=>$_POST['passp_num'],
							'fpassp_seria'=>$_POST['fpassp_seria'],	
							'fpassp_num'=>$_POST['fpassp_num'],	
							'stazh'=>$_POST['stazh'],	
							'email'=>$_POST['email'],
							'nal_number'=>$_POST['nal_number'],	
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->insert('_employeers', $insArray);
		$insRid = $this->db->insert_id();
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return $insRid;
		}		
	}
	
	public function UpdateRecord(){
		$updateArray = array('_filials_rid'=>$_POST['_filials_rid'],
							'f_name'=>$_POST['f_name'],
							's_name'=>$_POST['s_name'],
							'l_name'=>$_POST['l_name'],
							'f_name_lat'=>$_POST['f_name_lat'],
							'l_name_lat'=>$_POST['l_name_lat'],
							'birthday'=>$this->toMySQLDate($_POST['birthday']),
							'bdate'=>$this->toMySQLDate($_POST['bdate']),
							'edate'=>$this->toMySQLDate($_POST['edate']),
							'passp_seria'=>$_POST['passp_seria'],	
							'passp_num'=>$_POST['passp_num'],	
							'fpassp_seria'=>$_POST['fpassp_seria'],	
							'fpassp_num'=>$_POST['fpassp_num'],	
							'stazh'=>$_POST['stazh'],	
							'email'=>$_POST['email'],	
							'nal_number'=>$_POST['nal_number'],	
							'descr'=>$_POST['descr'],
							'archive'=>$_POST['archive'],
							'modifier_users_rid'=>$this->userRid,
							'modifyDT'=>$this->nowTime);
		$this->db->trans_begin();
		$this->db->update('_employeers', $updateArray, array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
	
	public function DeleteRecord(){
		$this->db->trans_begin();
		$this->db->delete('_employeers', array('rid'=>$this->ciObject->GetSessionData('activerecord')));
		if ($this->db->trans_status() === FALSE){
    		$this->db->trans_rollback();
    		return False;
		}else{
    		$this->db->trans_commit();
    		return True;
		}		
	}
}
?>